//
//  CollectionTableViewController.m
//  MoeFM
//
//  Created by miyang on 14-11-6.
//  Copyright (c) 2014年 StraightJet. All rights reserved.
//

#import "CollectionTableViewController.h"
#import "Favs.h"


@interface CollectionTableViewController ()

@property (nonatomic,strong)UIButton *radioBtn;
@property (nonatomic,strong)UIButton *musicBtn;
@property (nonatomic,strong)UIButton *songBtn;

@end

@implementation CollectionTableViewController

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        
        
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.navigationItem.title = @"我的收藏";
    
    _radioBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_radioBtn setTitle:@"电台" forState:UIControlStateNormal];
    [_radioBtn setTitleColor:[UIColor colorWithRed:127/255.0 green:230/255.0 blue:127/255.0 alpha:1] forState:UIControlStateNormal];
//    _radioBtn.backgroundColor = [UIColor blackColor];
    _radioBtn.frame = CGRectMake(20, 0, 90, 30);
    [_radioBtn addTarget:self action:@selector(radioView) forControlEvents:UIControlEventTouchUpInside];
    
    
    UIImageView *lineView1 = [[UIImageView alloc] initWithFrame:CGRectMake(113, 5, 1, 20)];
    [lineView1 setImage:[UIImage imageNamed:@"line.png"]];
    
    
    _musicBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_musicBtn setTitle:@"专辑" forState:UIControlStateNormal];
    [_musicBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//    _musicBtn.backgroundColor = [UIColor blackColor];
    _musicBtn.frame = CGRectMake(115, 0, 90, 30);
    [_musicBtn addTarget:self action:@selector(musicView) forControlEvents:UIControlEventTouchUpInside];
    
    UIImageView *lineView2 = [[UIImageView alloc] initWithFrame:CGRectMake(207, 5, 1, 20)];
    [lineView2 setImage:[UIImage imageNamed:@"line.png"]];
    
    
    _songBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_songBtn setTitle:@"曲目" forState:UIControlStateNormal];
    [_songBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//    _songBtn.backgroundColor = [UIColor blackColor];
    _songBtn.frame = CGRectMake(210, 0, 90, 30);
    [_songBtn addTarget:self action:@selector(songView) forControlEvents:UIControlEventTouchUpInside];
    
    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 30)];
    [headView addSubview:_radioBtn];
    [headView addSubview:lineView1];
    [headView addSubview:_musicBtn];
    [headView addSubview:lineView2];
    [headView addSubview:_songBtn];
    
    self.tableView.tableHeaderView = headView;
    
    _showarr = _radioarr;
    _type = ShowTyperadio;
    

}

- (void)viewDidAppear:(BOOL)animated
{
    if ([FavManager sharedManager].status == UserStatusLogOut) {
        
    }
}

- (void)radioView
{
    _showarr = _radioarr;
    _type = ShowTyperadio;
    [_radioBtn setTitleColor:[UIColor colorWithRed:127/255.0 green:230/255.0 blue:127/255.0 alpha:1] forState:UIControlStateNormal];
    [_musicBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_songBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.tableView reloadData];
    
}

- (void)musicView
{
    _showarr = _musicarr;
    _type = ShowTypemusic;
    [_radioBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_musicBtn setTitleColor:[UIColor colorWithRed:127/255.0 green:230/255.0 blue:127/255.0 alpha:1] forState:UIControlStateNormal];
    [_songBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.tableView reloadData];
}

- (void)songView
{
    _showarr = _songarr;
    _type = ShowTypesong;
    [_radioBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_musicBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_songBtn setTitleColor:[UIColor colorWithRed:127/255.0 green:230/255.0 blue:127/255.0 alpha:1] forState:UIControlStateNormal];
    [self.tableView reloadData];
}

#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

        return self.showarr.count;
    
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 64;
}

//- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
//{
//    if (section == 0) {
//        return @"电台收藏";
//    }else {
//        return @"专辑收藏";
//    }
//}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"collection"];
    //cell.selectionStyle = UITableViewCellSelectionStyleBlue;
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"collection"];
    }
    if (_type == ShowTyperadio || _type == ShowTypemusic) {
        Favs *fav = [[Favs alloc] init];
        fav = [_showarr objectAtIndex:indexPath.row];
        cell.textLabel.text = fav.wiki_title;
    } else if (_type == ShowTypesong) {
        Sub *subItem = [[Sub alloc] init];
        subItem = [_showarr objectAtIndex:indexPath.row];
        cell.textLabel.text = subItem.sub_title;
    }
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self performSelector:@selector(deselect) withObject:nil afterDelay:0.2f];
    
    if (_type == ShowTyperadio) {
        Favs *pass = [[Favs alloc] init];
        pass = [_radioarr objectAtIndex:indexPath.row];
        NSString *urlStr = [NSString stringWithFormat:RADIO_URL,pass.wiki_id];
//        NSString *albumStr = [NSString stringWithFormat:@"%@",pass.wiki_title];
        [[PlayerViewController sharPlayer] playingWithURL:urlStr andAlbumTitle:@"收藏电台"];
    } else if (_type == ShowTypemusic)
    {
        Favs *pass = [[Favs alloc] init];
        pass = [_musicarr objectAtIndex:indexPath.row];
        NSString *urlStr = [NSString stringWithFormat:MUSIC_URL,pass.wiki_id];
//        NSString *albumStr = [NSString stringWithFormat:@"%@",pass.wiki_title];
        [[PlayerViewController sharPlayer] playingWithURL:urlStr andAlbumTitle:@"收藏专辑"];
    } else {
        //NSMutableArray *subarr = [[NSMutableArray alloc] init];
        NSString *substr = [[NSString alloc] init];

        for (NSInteger i = indexPath.row; i < _showarr.count; i++) {
            Sub *subItem = [[Sub alloc] init];
            subItem = [_showarr objectAtIndex:i];
            //[subarr addObject:[NSString stringWithFormat:@"%ld",subItem.sub_id]];
            substr = [substr stringByAppendingFormat:@"%@,",subItem.sub_id];
        }
        NSString *urlStr = [NSString stringWithFormat:SONG_URL,substr];
//        NSString *albumStr = @"单曲";
        [[PlayerViewController sharPlayer] playingWithURL:urlStr andAlbumTitle:@"收藏单曲"];
    }
//    Favs *pass = [[Favs alloc] init];
//    if (indexPath.section == 0) {
//        pass = [_radioarr objectAtIndex:indexPath.row];
//        NSString *urlStr = [NSString stringWithFormat:RADIO_URL,pass.wiki_id];
//        NSString *albumStr = [NSString stringWithFormat:@"%@",pass.wiki_title];
//        [[PlayerViewController sharPlayer] playingWithURL:urlStr andAlbumTitle:albumStr andID:pass.wiki_id];
//    } else {
//        pass = [_musicarr objectAtIndex:indexPath.row];
//        NSString *urlStr = [NSString stringWithFormat:MUSIC_URL,pass.wiki_id];
//        NSString *albumStr = [NSString stringWithFormat:@"%@",pass.wiki_title];
//        [[PlayerViewController sharPlayer] playingWithURL:urlStr andAlbumTitle:albumStr andID:pass.wiki_id];
//    }
    
}

//- (void)request:(NSString *)path andParameters:(NSDictionary *)parameters
//{
//    
//    NSURLRequest *request = [OAuth1Controller preparedRequestForPath:path
//                                                          parameters:parameters
//                                                          HTTPmethod:@"GET"
//                                                          oauthToken:self.oauthToken
//                                                         oauthSecret:self.oauthTokenSecret];
//    AFHTTPRequestOperation *op = [[AFHTTPRequestOperation alloc] initWithRequest:request];
//    op.responseSerializer = [AFJSONResponseSerializer serializer];
//    [op setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
//        NSDictionary *returndic = [responseObject valueForKey:@"response"];
//        NSArray *favs = [returndic objectForKey:@"favs"];
//        
//        NSMutableArray *radioarr = [[NSMutableArray alloc] init];
//        NSMutableArray *musicarr = [[NSMutableArray alloc] init];
//        
//        
//        for (NSDictionary *favdic in favs) {
//            Favs *favobj = [[Favs alloc] init];
//            NSDictionary *obj = [favdic objectForKey:@"obj"];
//            favobj.wiki_name = [obj objectForKey:@"wiki_name"];
//            favobj.wiki_title = [obj objectForKey:@"wiki_title"];
//            favobj.wiki_meta = [obj objectForKey:@"wiki_meta"];
//            favobj.wiki_cover = [obj objectForKey:@"wiki_cover"];
//            favobj.wiki_id = [obj objectForKey:@"wiki_id"];
//            if ([[favdic valueForKey:@"fav_obj_type"] isEqualToString:@"radio"]) {
//                [radioarr addObject:favobj];
//            } else if ([[favdic valueForKey:@"fav_obj_type"] isEqualToString:@"music"]) {
//                [musicarr addObject:favobj];
//            }
//        }
//        _radioarr = radioarr;
//        _musicarr = musicarr;
//        [self.tableView reloadData];
//
//        
//    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//        NSLog(@"%@",error);
//    }];
//    
//    
//    [[NSOperationQueue mainQueue] addOperation:op];
//}

- (void)deselect
{
    [self.tableView deselectRowAtIndexPath:[self.tableView indexPathForSelectedRow] animated:YES];
}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
